/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tencent.mavenproject1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author HappyQin
 */
public class MailService {
 private static boolean ready = false;
 private static Properties prop = null;

 private static String smtpHost="smtp.163.com";
 private static String smtpAuth;
 private static String smtpUser="hujiangshui";
 private static String smtpPassword="hjs312net";
 private static String mailAddress="hujiangshui@163.com";

 private MailService() {
  init();
 }

 private static void init() {
  prop = new Properties();

//  FileInputStream fis = null;
//  try {
//   URL url = MailService.class.getClassLoader().getResource(
//     "smtp.config");
//   if (url != null) {
//    fis = new FileInputStream(url.getFile());
//    //System.out.println(url.getFile());
//   }
//  } catch (FileNotFoundException e) {
//   e.printStackTrace();
//  }
//  if (fis != null) {
//   try {
//    prop.load(fis);
//   } catch (IOException e1) {
//    e1.printStackTrace();
//   }
//  }
//  mailAddress = prop.getProperty("SMTP_EMAIL");
//  smtpPassword = prop.getProperty("SMTP_PWD");
//  smtpAuth = prop.getProperty("SMTP_AUTH");
//  smtpHost = prop.getProperty("SMTP_HOST");
//  smtpUser = prop.getProperty("SMTP_USER");

  ProxyConfig proxy = new ProxyConfig();
  if ((proxy.getSocksProxyEnabled() != null)
    && ("true".equalsIgnoreCase(proxy.getSocksProxyEnabled()))) {
   System.setProperty("proxyHost", proxy.getSocksProxyHost());
   System.setProperty("proxyPort", proxy.getSocksProxyPort());
   System.setProperty("proxyUserName", smtpUser);
   System.setProperty("proxyPassword", smtpPassword);

   System.setProperty("socksProxySet", "true");
   System.setProperty("socksProxyHost", proxy.getSocksProxyHost());
   System.setProperty("socksProxyPort", proxy.getSocksProxyPort());
   System.setProperty("socksProxyUserName", smtpUser);
   System.setProperty("socksProxyPassword", smtpPassword);

   System.setProperty("socks.useProxy", "true");
   System.setProperty("socks.proxyHost", proxy.getSocksProxyHost());
   System.setProperty("socks.proxyPort", proxy.getSocksProxyPort());
   System.setProperty("socks.proxyUserName", smtpUser);
   System.setProperty("socks.proxyPassword", smtpPassword);

   if (proxy.getHttpNonProxyHosts() != null)
    System.setProperty("socksNonProxyHosts", proxy
      .getSocksNonProxyHosts());
  }

  ready = true;
 }

 public static void sendMail(String to, String subject, String content) {
  if (!ready) {
   init();
  }

  Properties props = new Properties();

  props.put("mail.smtp.host", smtpHost);
  props.put("mail.smtp.port", "" + 25);
//  props.put("mail.smtp.auth", smtpAuth);
  props.put("mail.smtp.user", smtpUser);

  //props.put("mail.store.protocol", "pop3");
  //props.put("mail.transport.protocol", "smtp");

  Session session = Session.getDefaultInstance(props,
    new Authenticator() {
     public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(smtpUser,
        smtpPassword);
     }
    });
  MimeMessage message = new MimeMessage(session);
  try {
   message.setFrom(new InternetAddress(mailAddress));
   message.setRecipient(Message.RecipientType.TO, new InternetAddress(
     to));
   message.setSubject(subject);
   message.setContent(content, "text/plain;charset=gb2312");
   Transport.send(message);
  } catch (AddressException e) {
   e.printStackTrace();
  } catch (MessagingException e) {
   e.printStackTrace();
  }
 }

 public static void main(String args[]) {
  MailService.sendMail("hujiangshui@163.com", "Subject Test", "Mail Content");
 }
}